"use strict";
/* Controllers */
app.controller("healthScoreDetail2Controller", ['$scope', '$compile', '$location', '$rootScope', '$routeParams', 'healthScoreDetail2Service', function ($scope, $compile, $location, $rootScope, $routeParams, healthScoreDetail2Service) {

    var vm = $scope.vm = {};
    vm.p = {};
    var tag = $routeParams.tag;
    var dt = $routeParams.dt;
    var appid = $routeParams.appid;

    vm.p.tag = tag;
    vm.p.dt = dt;
    vm.p.dt2 = dt;
    vm.p.appId = appid;

    function getNewObjectOf(src) {
        return Object.assign({}, src);
    }

    if (vm.p.dt == null || typeof (vm.p.dt) == 'undefined') {
        vm.p.dt = new Date(new Date().getTime() - 86400000).format('yyyy-MM-dd');
        vm.p.dt2 = vm.p.dt;
    }
    if (typeof (vm.p.appId) == "undefined" || vm.p.appId == null) {
        vm.p.appId = 'all';
    }


    var errorFn = function (result) {
        alert(result);
    };

    $scope.getHealthScoreAppid = function () {
        var vm = $scope.vm;
        var params = vm.p;

        var procFn = function (res) {
            if (res.success && res.data) {
                if(res.data.length!=1) {
                    console.log("data item is invalid. size: " + res.data.length);
                }
                vm.p = res.data[0];
                var splitContent = function(content) {
                    if (typeof(content) == 'undefined') {
                        return [];
                    }
                    var items = content.split(',');
                    var arr = [];
                    for(var i=0;i<items.length;) {
                        var it = {};
                        it.key = items[i++];
                        it.val = items[i++];
                        arr.push(it);
                    }
                    return arr;
                }

                var splitReason = function(reason) {
                    if (typeof(reason) == 'undefined') {
                        return [];
                    }
                    var rows = reason.split(',');
                    var arr = [];
                    for(var i=0;i<rows.length;++i) {
                        var items = rows[i].split(':');
                        var it = {};
                        it.key = items[1];
                        it.val = items[2];
                        arr.push(it);
                    }
                    return arr;
                }
                vm.p.dataContentItems = splitContent(vm.p.dataContent);
                vm.p.healthReasonItems= splitReason(vm.p.healthReason);
                vm.p.appidUri = buildAppidUri(vm.p.tag, vm.p.appId);
            } else {
                alert("暂无数据");
            }
        };
        return healthScoreDetail2Service.listHealthScoreAppid(params, procFn, errorFn);
    };



    $scope.initializeController = function () {
        var vm = $scope.vm;

        $scope.getHealthScoreAppid();
    };

    $scope.initializeController();
}]);

/* Services */
app.service('healthScoreDetail2Service', ['ajaxService', function (ajaxService) {

    this.listHealthScoreAppid = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/healthScore/listHealthScoreAppid.do", successFunction, errorFunction);
    };

}]);

